﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.segments.field;

namespace ConsoleApplication1.segments
{
    class OBX
    {
        public StringBuilder set_ID_OBX { get; set; }
        public StringBuilder value_type { get; set; }
        public CE observation_identifier { get; set; }
        public StringBuilder observation_value { get; set; }
        public StringBuilder units { get; set; }
        public StringBuilder References_Range { get; set; }

        public OBX()
        {
            set_ID_OBX = new StringBuilder();
            value_type = new StringBuilder();
            observation_identifier = new CE();
            observation_value = new StringBuilder();
            units = new StringBuilder();
            References_Range = new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i ,x=0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    set_ID_OBX.Capacity = set_ID_OBX.Length;
                    value_type.Capacity = value_type.Length;
                    observation_value.Capacity = observation_value.Length;
                    units.Capacity = units.Length;
                    return i - 1;
                }
                else if (source[i] == '|')
                {
                    if (x == 6)
                    {
                        set_ID_OBX.Capacity = set_ID_OBX.Length;
                        value_type.Capacity = value_type.Length;
                        observation_value.Capacity = observation_value.Length;
                        units.Capacity = units.Length;
                        return i;
                    }
                    else
                    {
                        x++;
                        continue;
                    }
                }
                else
                {
                    if (x == 1)  // passing prameter of set id - PID
                    {
                        if (source[i] != '|')
                        {
                            if (set_ID_OBX == null)
                                set_ID_OBX = new StringBuilder();
                            set_ID_OBX.Insert(set_ID_OBX.Length, source[i]);
                        }
                    }
                    else if (x == 2)
                    {
                        if (source[i] != '|')
                        {
                            if (value_type == null)
                                value_type = new StringBuilder();
                            value_type.Insert(value_type.Length, source[i]);
                        }
                    }
                    else if (x == 3)
                    {
                        if (source[i] != '|')
                        {
                            if (observation_identifier == null)
                                observation_identifier = new  CE ();
                            i = observation_identifier.fill(source, i);
                        }
                    }
                    else if (x == 4)
                    {
                        observation_value.Insert(observation_value.Length, source[i]);
                    }
                    else if (x == 5)
                    {
                        units.Insert(units.Length, source[i]);
                    }
                    else if (x == 6)
                    {
                        References_Range.Insert(References_Range.Length, source[i]);
                    }
                 }                                  
            }
            return i;

        }

        public override string ToString()
        {
            string obx_string = "OBX";

            if (set_ID_OBX.Length != 0)
                obx_string += "|" + set_ID_OBX;
            if (value_type.Length != 0)
                obx_string += "|" + value_type;
            if (observation_identifier != null)
                obx_string += "|" + observation_identifier.ToString();
            if (observation_value.Length != 0)
                obx_string += "|" + observation_value;
            if (units != null)
                obx_string += "|" + units;
            if (References_Range != null)
                obx_string += "|" + References_Range;

            return obx_string;
        }
    }
}
